﻿namespace H.Net.Nets
{
    /// <summary>
    /// 表示一个会自动扩容的缓冲区
    /// </summary>
    internal class HWebFrame
    {
        /// <summary>
        /// 读取或设置主题ID
        /// </summary>
        public int TopicID { get; set; }

        /// <summary>
        /// 读取或设置类型ID
        /// </summary>
        public int TypeID { get; set; }

        /// <summary>
        /// 内部管理的缓冲区,请使用GetBuffer来修改Buffer的大小
        /// </summary>
        public byte[] Buffer { get; private set; } = new byte[100];

        /// <summary>
        /// 表示当前已经写入的数据所占的大小
        /// </summary>
        public int DataLength { get; set; } = 0;

        /// <summary>
        /// 获取一个大小足够大的缓冲区,以便使用
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public byte[] EnsureBufferSize(int size)
        {
            if (Buffer.Length < size)
            {
                Buffer = new byte[size];
            }
            return Buffer;
        }

        public override string ToString()
        {
            return $" 主题ID:{TopicID}\r\n 类型ID:{TypeID.ToString("x2")}\r\n   帧长度:{DataLength}";
        }
    }
}
